package com.example.zhu.controller;

import com.example.zhu.dto.EditAddressDTO;
import com.example.zhu.result.Result;
import com.example.zhu.vo.UserVo;
import com.example.zhu.service.AddressBookService;
import com.example.zhu.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
@CrossOrigin
public class UsersController {

    @Autowired
    private UsersService usersService;
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 查询所有用户信息
     * @return
     */
    @GetMapping()
    private Result<List<UserVo>> UserAll(){
//        List<UserVo> UserVolist = addressBookService.selectAllAddress();
//        return Result.success(UserVolist);
        return Result.success(null);
    }


    /**
     * 弹出编辑窗口,回显信息
     * @return
     */
    @GetMapping("/getDA")
    public Result<UserVo> getDA(Integer id){
        UserVo outOrderVO = addressBookService.getDA(id);
        return Result.success(outOrderVO);
    }


    /**
     * 编辑信息
     * @return
     */
    @PutMapping("/saveInfo")
    public Result<String> editAddress(@RequestBody EditAddressDTO editAddressDTO){
        Result<String> result = addressBookService.editInfo(editAddressDTO);
        return result;
    }

    /**
     * 根据id删除用户信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Integer id){
        addressBookService.deleteAddress(id);
        return Result.success("删除成功!");
    }

    /**
     * 根据手机号查询用户信息
     * @param phone
     * @return
     */
    @GetMapping("/getByPhone")
    public Result<List<UserVo>> getByPhone(String phone){
        List<UserVo> userVo = addressBookService.getByPhone(phone);
        return Result.success(userVo);
    }

    /**
     * 根据楼号查询用户
     * @param areas
     * @return
     */
    @PostMapping
    public Result<List<UserVo>> getByAreas(@RequestBody  List<String> areas){
        if (areas!=null){
            List<UserVo> list = addressBookService.getByAreas(areas);
            return Result.success(list);
        }else {
            return Result.error("参数不能为空");
        }
    }

//    /**
//     * 根据id
//     * @param id
//     * @return
//     */
//   @GetMapping("/change")
//   private Result change(Integer id){
//       usersService.updateUser(id);
//       return Result.success();
//   }
}
